//
// Created by zhupi on 2024/9/29.
//

#include "project_dock.h"
#include "project_tree_view_ctrl.h"
#include <QStyleFactory>

ProjectDock::ProjectDock(QWidget *parent): QDockWidget(parent) { }

void ProjectDock::Initial() {
    Test1();
}

void ProjectDock::Test1() {
    // 创建 TreeModel 实例
    TreeModel* model = new TreeModel();

    // 添加节点（示例：16个节点，4层深度）
    model->AddTree({1, 30, "Root1", "TypeRoot"});

    model->AddTree({3, 1, "Child1-1", "TypeChild"});
    model->AddTree({4, 1, "Child1-2", "TypeChild"});
    model->AddTree({5, 2, "Child2-1", "TypeChild"});
    model->AddTree({6, 2, "Child2-2", "TypeChild"});



    model->AddTree({9, 4, "Child1-2-1", "TypeChild"});
    model->AddTree({10, 5, "Child2-1-1", "TypeChild"});

    model->AddTree({14, 8, "Child1-1-2-1", "TypeLeaf"});
    model->AddTree({15, 9, "Child1-2-1-1", "TypeLeaf"});
    model->AddTree({16, 10, "Child2-1-1-1", "TypeLeaf"});


    model->AddTree({7, 3, "bbbbbbbbb", "TypeChild"});
    model->AddTree({8, 3, "aaaaa", "TypeChild"});

    model->AddTree({2, 30, "Root2", "TypeRoot"});


    model->AddTree({11, 6, "Child2-2-1", "TypeChild"});
    model->AddTree({12, 6, "Child2-2-2", "TypeChild"});
    model->AddTree({13, 7, "Child1-1-1-1", "TypeLeaf"});
    // 创建 QTreeView 并设置模型
    TreeBaseWidget* treeView = new TreeBaseWidget(this);
    treeView->setStyle(QStyleFactory::create("windows"));
    treeView->setModel(model);
    treeView->setWindowTitle("TreeView Example - 4 Levels, 16 Nodes");
    this->setWidget(treeView);



//    TreeItemDelegate *delegate = new TreeItemDelegate(treeView);
//    treeView->setItemDelegate(delegate);

//     设置 QTreeView 的属性
    treeView->setItemsExpandable(true);
    treeView->setExpandsOnDoubleClick(false); // 设置只能通过点击 + 或 - 号展开和收缩
    treeView->setRootIsDecorated(true); // 显示 + 和 - 号的开关
//    treeView->setIndentation(30); // 设置缩进（对齐用虚线）

// 禁用 ID 为 1 的节点
    model->SetItemDisabled(1, true);

// 设置 ID 为 2 的节点字体为粗体、颜色为红色
    QFont font;
    font.setBold(true);
    model->SetItemFontAndColor(2, font, Qt::red);


    // 连接点击信号到槽，打印节点ID
//    QObject::connect(treeView, &QTreeView::clicked, [](const QModelIndex& index) {
//        TreeNode* node = static_cast<TreeNode*>(index.internalPointer());
//        if (node) {
//            qDebug() << "Node clicked, ID:" << node->data_.id_ << "   " << node->data_.type_;
//        }
//    });


}
